package cn.zhagzheguo.xk.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.zhagzheguo.xk.dao.CourseSelectDao;
import cn.zhagzheguo.xk.model.Course;
import cn.zhagzheguo.xk.model.CourseSelect;
import cn.zhagzheguo.xk.status.CommonStatus;

@Repository
public class CourseSelectDaoImpl implements CourseSelectDao {

	@Autowired
	SqlSessionFactory sqlSessionFactory;

	@Override
	public int insert(CourseSelect courseSelect) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			session.insert("cn.zhagzheguo.xk.mapper.CourseSelectMapper.insert", courseSelect);
			session.commit();
			return CommonStatus.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
			return CommonStatus.FAIL;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<Course> getBriefByStuId(int id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			List<Course> list = session.selectList("cn.zhagzheguo.xk.mapper.CourseSelectMapper.selectList_brief", id);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<CourseSelect> getDetailByStuId(int id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			List<CourseSelect> list = session.selectList(
					"cn.zhagzheguo.xk.mapper.CourseSelectMapper.selectList_detail", id);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public CourseSelect getByStuIdAndCourseId(int stu_id, int course_id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("stu_id", stu_id);
			map.put("course_id", course_id);

			CourseSelect cs = session.selectOne("cn.zhagzheguo.xk.mapper.CourseSelectMapper.selectOne", map);
			return cs;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int delete(int stu_id, int course_id) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("stu_id", stu_id);
			map.put("course_id", course_id);

			session.delete("cn.zhagzheguo.xk.mapper.CourseSelectMapper.delete", map);
			session.commit();
			return CommonStatus.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
			return CommonStatus.FAIL;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public static void main(String[] args) {
		CourseSelectDao cs = new CourseSelectDaoImpl();
		List<Course> list = cs.getBriefByStuId(1);
		System.out.println(list.toString());
	}

}
